﻿<!-- 正文开始 -->
<div class="layui-fluid">
    <div class="layui-card">
        <div class="layui-card-body">
            <div class="layui-form toolbar">
                <div class="layui-form-item">
                    <div class="layui-inline">
                        <label class="layui-form-label w-auto">搜索：</label>
                        <div class="layui-input-inline mr0">
                            <input name="keyword" class="layui-input" type="text" placeholder="输入关键字"/>
                        </div>
                    </div>
                    <div class="layui-inline">
                        <button class="layui-btn icon-btn" lay-filter="formSubSearchServerMap" lay-submit>
                            <i class="layui-icon">&#xe615;</i>搜索
                        </button>
                        <button id="btnAddTDTServerMap" class="layui-btn icon-btn"><i class="layui-icon">&#xe654;</i>添加天地图</button>
                        <button id="btnAddCustomServerMap" class="layui-btn icon-btn"><i class="layui-icon">&#xe654;</i>添加【自定义】</button>
                    </div>
                </div>
            </div>
            <table class="layui-table" id="tableServerMap" lay-filter="tableServerMap"></table>
        </div>
    </div>
</div>

<!-- 表格操作列 -->
<script type="text/html" id="tableBarServerMap">
    <a class="layui-btn layui-btn-primary layui-btn-xs" lay-event="edit">修改</a>
    <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>
</script>
<!-- 表单弹窗 -->
<script type="text/html" id="modelServerMap">
    <form id="modelServerMapForm" lay-filter="modelServerMapForm" class="layui-form model-form">
        <input name="id" type="hidden"/>
		<input name="provider" type="hidden" value="tianditu"/>
		<input name="serverType" type="hidden" value="url"/>
        <div class="layui-form-item" style="text-align: center;">
			<img id="serverMap-thumbnail-img" src="#(ctx)/assets/img/thum-default.jpg" style="width:96px;height:96px;">
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">授权</label>
            <div class="layui-input-block">
                <input name="auth" placeholder="请输入授权Key" type="text" class="layui-input" maxlength="50"
                       lay-verType="tips" lay-verify="required" required/>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">名称</label>
            <div class="layui-input-block">
                <input name="name" placeholder="请输入名称" type="text" class="layui-input" maxlength="50"
                       lay-verType="tips" lay-verify="required" required/>
            </div>
        </div>
		<div class="layui-form-item">
		    <label class="layui-form-label">标识</label>
		    <div class="layui-input-block">
		      <select name="type" lay-filter="type">
		        <option value=""></option>
				<option value="img_w">全球影像</option>
		        <option value="ibo_w">全球行政边界</option>
				<option value="vec_w">全球矢量图</option>
				<option value="cia_w">中文道路标注</option>
				<option value="swdx">全球地形</option>
				<option value="ter_w">全球地形晕渲</option>
		      </select>
		    </div>
		</div>
		<div class="layui-form-item">
		    <label class="layui-form-label">分类</label>
		    <div class="layui-input-block">
		      <select name="classify" lay-filter="classify">
		        <option value=""></option>
		        <option value="imagery">底图</option>
				<option value="road">道路</option>
				<option value="terrain">地形</option>
				<option value="area">行政区</option>
		      </select>
		    </div>
		</div>
		<div class="layui-form-item">
            <label class="layui-form-label">备注</label>
            <div class="layui-input-block">
                <textarea name="remark" placeholder="请输入内容" class="layui-textarea" ></textarea>
            </div>
		</div>
        <div class="layui-form-item text-right">
            <button class="layui-btn layui-btn-primary" type="button" ew-event="closeDialog">取消</button>
            <button class="layui-btn" lay-filter="modelSubmitServerMap" lay-submit>保存</button>
        </div>
    </form>
</script>

<!-- 表单弹窗 -->
<script type="text/html" id="modelCustomServerMapScript">
    <form id="modelServerMapForm" lay-filter="modelServerMapForm" class="layui-form model-form">
        <input name="id" type="hidden"/>
		<input name="provider" type="hidden" value="custom"/>
        <div class="layui-form-item" style="text-align: center;">
			<img id="serverMap-thumbnail-img" src="#(ctx)/assets/img/thum-default.jpg" style="width:96px;height:96px;">
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">名称</label>
            <div class="layui-input-block">
                <input name="name" placeholder="请输入名称" type="text" class="layui-input" maxlength="50"
                       lay-verType="tips" lay-verify="required" required/>
            </div>
        </div>
		<div class="layui-form-item">
		    <label class="layui-form-label">类型</label>
		    <div class="layui-input-block">
		      <select name="serverType" lay-filter="serverType">
		        <option value=""></option>
		        <option value="url">url</option>
				<option value="wmts">wmts</option>
				<option value="tms">tms</option>
		      </select>
		    </div>
		</div>
		<div class="layui-form-item">
		    <label class="layui-form-label">分类</label>
		    <div class="layui-input-block">
		      <select name="classify" lay-filter="classify">
		        <option value=""></option>
		        <option value="imagery">底图</option>
				<option value="road">道路</option>
				<option value="terrain">地形</option>
				<option value="area">行政区</option>
		      </select>
		    </div>
		</div>
		<div class="layui-form-item">
            <label class="layui-form-label">参数</label>
            <div class="layui-input-block">
                <textarea name="options" placeholder="请输入内容" class="layui-textarea" ></textarea>
            </div>
		</div>
		<div class="layui-form-item">
            <label class="layui-form-label">备注</label>
            <div class="layui-input-block">
                <textarea name="remark" placeholder="请输入内容" class="layui-textarea" ></textarea>
            </div>
		</div>
        <div class="layui-form-item text-right">
            <button class="layui-btn layui-btn-primary" type="button" ew-event="closeDialog">取消</button>
            <button class="layui-btn" lay-filter="modelSubmitServerMap" lay-submit>保存</button>
        </div>
    </form>
</script>

<!-- 表单弹窗 -->
<script type="text/html" id="modelServerMapData">
    <div class="layui-card">
        <div class="layui-card-body">
            <table class="layui-table" id="tableServerMapData" lay-filter="tableServerMapData"></table>
        </div>
    </div>
</script>
<!-- 表格操作列 -->
<script type="text/html" id="tableBarServerMapData">
    <a class="layui-btn layui-btn-primary layui-btn-xs" lay-event="authData">授权</a>
    <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="cancelData">取消</a>
</script>


<!-- js部分 -->
<script>
layui.use(['layer', 'form', 'table', 'util', 'admin', 'zTree','treetable', 'config','upload'], function () {
        var $ = layui.jquery;
        var layer = layui.layer;
        var form = layui.form;
        var table = layui.table;
        var util = layui.util;
        var admin = layui.admin;
        var config = layui.config;
        var treetable = layui.treetable;
        var config = layui.config;
        var upload = layui.upload;
        
        // 渲染表格
        var insTb = table.render({
            elem: '#tableServerMap',
            url: '#(ctx)/resources/server/map/listData',
            page: true,
            cellMinWidth: 100,
            cols: [[
                {type: 'numbers'},
                {field: 'thumbnail', sort: false, title: '缩略图'},
                {field: 'name', sort: false, title: '名称'},
                {field: 'provider', sort: false, title: '服务商'},
                {field: 'serverType', sort: false, title: '服务类型'},
                {field: 'classify', sort: false, title: '类型'},
                {field: 'isPublic', sort: false, title: '是否公开'},
                {align: 'center', toolbar: '#tableBarServerMap', title: '操作', minWidth: 150}
            ]]
        });

        // 添加
        $('#btnAddTDTServerMap').click(function () {
            showEditModel();
        });
        $('#btnAddCustomServerMap').click(function () {
        	showCustomEditModel();
        });       
        

        // 搜索
        form.on('submit(formSubSearchServerMap)', function (data) {
            insTb.reload({where: data.field}, 'data');
        });

        // 工具条点击事件
        table.on('tool(tableServerMap)', function (obj) {
            var data = obj.data;
            var layEvent = obj.event;
            if (layEvent === 'edit') { // 修改
            	if(data.provider=='tianditu'){
            		 showEditModel(data);
            	}else{
            		showCustomEditModel(data);
            	}
            } else if (layEvent === 'del') { // 删除
                doDel(obj);
            }
            
        });

        // 删除
        function doDel(obj) {
            layer.confirm('确定要删除“' + obj.data.name + '”服务吗？', {
                skin: 'layui-layer-admin',
                shade: .1
            }, function (i) {
                layer.close(i);
                layer.load(2);
                admin.req('resources/server/map/delete', {
                    id: obj.data.id
                }, function (res) {
                    layer.closeAll('loading');
                    if (res.code == 200) {
                        layer.msg(res.message, {icon: 1});
                        obj.del();
                    } else {
                        layer.msg(res.message, {icon: 2});
                    }
                }, 'get');
            });
        }

        // 显示编辑弹窗
        function showEditModel(mServerMap) {
            admin.open({
                type: 1,
                offset: 't',
                title: (mServerMap ? '修改' : '添加') + '服务',
                content: $('#modelServerMap').html(),
                success: function (layero, dIndex) {
                    var url = 'resources/server/map/save';
                    form.val('modelServerMapForm', mServerMap);  // 回显数据
    				if (mServerMap && mServerMap.thumbnail) {
    					$('#serverMap-thumbnail-img').attr("src", "#(ctx)" + mServerMap.thumbnail);
    				}
                    if(mServerMap){
        				upload.render({
        					elem: '#serverMap-thumbnail-img'
        					, url: '#(ctx)/resources/server/map/uploadThumbnail?id=' + mServerMap.id
        					, accept: 'images'
        					, before: function(obj) {
        						layer.load();
        					}
        					, done: function(res, index, upload) {
        						layer.closeAll('loading'); //关闭loading
        						if (200 == res.code) {
        							layer.msg(res.message, { icon: 1 });
        	    					$('#serverMap-thumbnail-img').attr("src", "#(ctx)" + res.data.thumbnail);
        						} else {
        							layer.msg(res.message, { icon: 2 });
        						}
        					}
        					, error: function(index, upload) {
        						layer.closeAll('loading');
        						layer.msg("上传失败！", { icon: 2 });
        					}
        				});
                    }

                    
                    // 表单提交事件
                    form.on('submit(modelSubmitServerMap)', function (data) {
                        layer.load(2);
                        var formData={}; 
                        for(var key in data.field){
                        	if ('file' !== key)
                        		formData['serverMap.'+key]=data.field[key];
                        }
                        admin.req(url, formData, function (res) {
                            layer.closeAll('loading');
                            if (res.code == 200) {
                                layer.close(dIndex);
                                layer.msg(res.message, {icon: 1});
                                insTb.reload({}, 'data');
                            } else {
                                layer.msg(res.message, {icon: 2});
                            }
                        }, 'get');
                        return false;
                    });
                }
            });
        }
        
        function showCustomEditModel(mServerMap) {
            admin.open({
                type: 1,
                offset: 't',
                title: (mServerMap ? '修改' : '添加') + '服务',
                content: $('#modelCustomServerMapScript').html(),
                success: function (layero, dIndex) {
                    var url = 'resources/server/map/save';
                    form.val('modelServerMapForm', mServerMap);  // 回显数据
    				if (mServerMap && mServerMap.thumbnail) {
    					$('#serverMap-thumbnail-img').attr("src", "#(ctx)" + mServerMap.thumbnail);
    				}
                    if(mServerMap){
        				upload.render({
        					elem: '#serverMap-thumbnail-img'
        					, url: '#(ctx)/resources/server/map/uploadThumbnail?id=' + mServerMap.id
        					, accept: 'images'
        					, before: function(obj) {
        						layer.load();
        					}
        					, done: function(res, index, upload) {
        						layer.closeAll('loading'); //关闭loading
        						if (200 == res.code) {
        							layer.msg(res.message, { icon: 1 });
        	    					$('#serverMap-thumbnail-img').attr("src", "#(ctx)" + res.data.thumbnail);
        						} else {
        							layer.msg(res.message, { icon: 2 });
        						}
        					}
        					, error: function(index, upload) {
        						layer.closeAll('loading');
        						layer.msg("上传失败！", { icon: 2 });
        					}
        				});
                    }

                    
                    // 表单提交事件
                    form.on('submit(modelSubmitServerMap)', function (data) {
                        layer.load(2);
                        var formData={}; 
                        for(var key in data.field){
                        	if ('file' !== key)
                        		formData['serverMap.'+key]=data.field[key];
                        }
                        admin.req(url, formData, function (res) {
                            layer.closeAll('loading');
                            if (res.code == 200) {
                                layer.close(dIndex);
                                layer.msg(res.message, {icon: 1});
                                insTb.reload({}, 'data');
                            } else {
                                layer.msg(res.message, {icon: 2});
                            }
                        }, 'get');
                        return false;
                    });
                }
            });
        }        
        
        
    });
</script>